<?php

namespace App\Traits;

use App\Model\User;
use Hash;

trait Password
{
    public function checkPassword(User $user, $password)
    {
        $this->password($user, $password, 'password', '手机号或密码错误');
    }

    public function checkPayPassword(User $user, $payPassword)
    {
        $this->password($user, $payPassword, 'pay_password', '密码错误');
    }

    private function password(User $user, $password, $type, $error)
    {
        if (! Hash::check($password, $user->$type)) {
            return response()->json($error, 400);
            $user->$type = Hash::make($password);
            if (! $user->save()) {
                return response()->json(['网络异常，请重试'], 500);
            }
        }
    }
}